package com.example.demo;

import org.jim.common.ImConfig;
import org.jim.common.config.PropertyImConfigBuilder;
import org.jim.common.packets.Command;
import org.jim.server.ImServerStarter;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.HandshakeReqHandler;
import org.jim.server.command.handler.LoginReqHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.command.MyLoginServiceProcessor;
import com.example.demo.command.MyWsHandshakeProcessor;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableTransactionManagement
@RestController
public class Application {

	public static void main(String[] args) {
		// 设置环境变量，解决Es的netty与Netty服务本身不兼容问题
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public void startJim() throws Exception {
		ImConfig imConfig = new PropertyImConfigBuilder("jim.properties").build();
		ImServerStarter imServerStarter = new ImServerStarter(imConfig);
		// start 以下处理器根据业务需要自行添加与扩展，每个Command都可以添加扩展,此处为demo中处理
		// 添加自定义握手处理器;
		HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ,
				HandshakeReqHandler.class);
		handshakeReqHandler.addProcessor(new MyWsHandshakeProcessor());

		// 添加登录业务处理器;
		LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
		loginReqHandler.addProcessor(new MyLoginServiceProcessor());
		// end******************************************************************************************/
		imServerStarter.start();
	}

}
